<template>
	<view class="background" style="box-sizing: border-box;padding-bottom: 180rpx;" v-show="show">
		<view class="daowu flexleft" @click="changetype">
			<view :class="buy_type==1?'wuliu':'daodian'">到店自提</view>
			<view :class="buy_type==2?'wuliu':'daodian'">物流配送</view>
		</view>
		<!--收货-->
		<view class="addshouhuo" @click="goadddizhi" v-if="!dataAll.addressData.id && buy_type==2">点击添加收货地址</view>
		<view class="flexbetween mainpadding ffffff margin-top" v-if="dataAll.addressData.id && buy_type==2" @click="goadddizhi">
			<view class="border-r1">
				<view class="name">{{dataAll.addressData.name}}</view>
				<text class="name tel">{{dataAll.addressData.mobile}}</text>
				<view class="address">{{dataAll.addressData.cityinfo}}{{dataAll.addressData.address_detail}}</view>
			</view>
			<view class="right_icon"></view>
		</view>
		<!---中间item-->
		<view class="" v-for="item in dataAll.orderData.lists" :key="item.shop_id">
			<view class="border-r1 mainpadding ffffff margin-top" v-if="buy_type==1">
				<view class="name">{{dataAll.shop_address.name}}</view>
				<view class="address">{{dataAll.shop_address.address}}</view>
			</view>
			<view class="itemlist" v-for="ite in item.products" :key="ite.id">
				<view class="productimg">
					<image :src="ite.image_text"></image>
				</view>
				<view class="right">
					<view class="righttop">
						<view class="txt1 yhxiankuan">{{ite.title}}</view>
						<view class="txt2">¥{{ite.sku.price}} </view>
					</view>
					<view class="rightbottom">
						<view class="txt3">{{ite.sku.difference.join()}}</view>
						<view class="txt4">x{{ite.number}}</view>
					</view>
				</view>
			</view>
			<view class="yunshu flexbetween" v-if="buy_type==2">
				<view class="yunfei">运费</view>
				<view class="baoyou">{{item.freight_price_bak}}</view>
			</view>
			<view class="yunshu flexbetween">
				<view class="yunfei">备注</view>
				<input type="text" style="text-align: right;" v-model="item.memo" placeholder="请输入留言信息给卖家">
			</view>
		</view>
		<view class="bottomsubmitbtn flexbetween">
			<view class="flexcenter">
				<view class="zongji">合计</view>
				<view class="price1" v-if="dataAll.orderData.statis.allsub">￥{{dataAll.orderData.statis.allsub}}</view>
			</view>
			<view class="submitcontext" @click="gopay">立即支付</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				buy_type: 2, //1到店自取，2物流
				address_id: "",
				type: "", //detail=商品详情，cart=购物车
				data: [],
				dataAll: {},
				
				show:false,
			};
		},
		onLoad(options) {
			//接收对象
			let data = JSON.parse(decodeURIComponent(options.msg));
			this.type = data.type
			this.data = data.data
		},
		onShow() {
			uni.$off("changeaddress")
			uni.$on("changeaddress", data => {
				this.address_id = data
			})
			this.init()
		},
		methods: {
			init() {
				let _this = this;
				httpRequest.request('api/goodsorder/getOrderGoodsList', 'POST', {
					type: this.type,
					buy_type: this.buy_type,
					address_id: this.address_id,
					data: this.data,
				}, false, false, false).then(res => {
					this.show=true
					if (res.code == 1) {
						res.data.orderData.lists.forEach(item=>{
							this.$set(item,"memo","")
						})
						this.dataAll = res.data
						this.address_id = res.data.addressData.id
					}else if(res.code==0){
						httpRequest.toast(res.msg)
						setTimeout(()=>{
							uni.navigateBack(1)
						},1000)
					}
				}).catch(err => {
					this.show=true
					httpRequest.toast(err.msg);
				})
			},
			gopay() {
				let _this=this
				if(!_this.dataAll.addressData.id &&_this.buy_type==2){
					uni.hideLoading()
					httpRequest.toast('请补全收货人姓名!');
					return false;
				}
				if(this.type=="cart"){
					httpRequest.request('api/cart/cearCart', 'POST', {
						shop_id: this.dataAll.shop_address.id,
					}, false, false, true).then(res => {
					})
				}
				let data = {
					buy_type: this.buy_type,
					address_id: this.address_id || "",
					lists: [],
				}
				this.dataAll.orderData.lists.forEach(item=>{
					let arr = {
						shop_id:item.shop_id,
						remarks:item.memo,
						products:[]
					}
					item.products.forEach(ite=>{
						arr.products.push({
							goods_id:ite.id,
							number:ite.number,
							sku_id:ite.sku.id
						})
						
					})
					data.lists.push(arr)
				})
				httpRequest.request('api/goodsorder/addOrder', 'POST', data, false, false, false).then(res => {
					console.log(res)
					if (res.code == 1) {
						let data = {
							id:res.data.order_pay_no,
							pay_price:res.data.pay_price,
							type:3,
							allscore:res.data.total_allow_score
						}
						uni.redirectTo({
							url:'/pages/hotel/hotelorder?msg=' + encodeURIComponent(JSON.stringify(data))
						})
					}
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			
			goadddizhi() {
				uni.navigateTo({
					url: '/pages/shopping/shouhuodizhi?type=1'
				})
			},
			changetype() {
				this.buy_type = this.buy_type == 1 ? 2 : 1
				this.init()
			}
		}
	}
</script>

<style lang="scss">
	.name {
		display: inline-block;
		font-size: 30rpx;
		font-family: PingFang SC-Heavy, PingFang SC;
		font-weight: 800;
		color: #000000;
	}

	.tel {
		margin-left: 50rpx;
	}

	.address {
		font-size: 28rpx;
		font-family: PingFang SC-Heavy, PingFang SC;
		font-weight: 500;
		color: #666666;
		margin-top: 30rpx;
	}

	.daowu {
		background-color: rgba(212, 212, 212, 1);
		border-radius: 50rpx;
		width: 300rpx;
	}

	.daodian {
		font-size: 26rpx;
		font-family: PingFang SC-Heavy, PingFang SC;
		font-weight: 800;
		color: #000000;
		width: 150rpx;
		text-align: center;
		padding: 11rpx 0;
	}

	.wuliu {
		width: 150rpx;
		padding: 11rpx 0;
		border-radius: 50rpx;
		font-size: 26rpx;
		font-weight: 800;
		color: #FFFFFF;
		text-align: center;
		background-color: rgba(0, 210, 225, 1);
	}

	.addshouhuo {
		margin-top: 30rpx;
		font-size: 30rpx;
		font-family: PingFang SC-Heavy, PingFang SC;
		font-weight: 800;
		color: #00D2E1;
		line-height: 42rpx;
		text-align: center;
		background-color: #FFFFFF;
		padding: 42rpx 0;
	}

	/*中间商品列表*/
	.itemlist {
		margin-top: 30rpx;
		background-color: #FFFFFF;
		padding: 30rpx;
		display: flex;
	}

	.productimg {
		width: 124rpx;
		height: 124rpx;
		margin-right: 25rpx;

		image {
			width: 100%;
			height: 100%;
			border-radius: 14rpx;
		}
	}

	.right {
		flex: 1;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
	}

	.righttop {
		display: flex;
		align-items: flex-start;
		justify-content: space-between;
	}

	.rightbottom {
		display: flex;
		align-items: flex-end;
		justify-content: space-between;
	}

	.txt1 {
		font-size: 30rpx;
		font-family: PingFang SC-Heavy, PingFang SC;
		font-weight: 800;
		color: #333333;
		line-height: 35rpx;
	}

	.txt2 {
		font-size: 34rpx;
		font-family: PingFang SC-Heavy, PingFang SC;
		font-weight: 800;
		color: #FF5C00;
		line-height: 40rpx;
	}

	.txt3 {
		font-size: 26rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #666666;
		line-height: 30rpx;
	}

	.txt4 {
		font-size: 26rpx;
		font-family: PingFang SC-Heavy, PingFang SC;
		font-weight: 800;
		color: #FF5C00;
		line-height: 30rpx;
	}

	/*运输*/
	.yunshu {
		margin-top: 30rpx;
		padding: 30rpx;
		background-color: #FFFFFF;
	}

	.yunfei {
		font-size: 26rpx;
		font-family: PingFang SC-Heavy, PingFang SC;
		font-weight: 800;
		color: #333333;
		line-height: 30rpx;
	}

	.baoyou {
		font-size: 26rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #666666;
		line-height: 30rpx;
	}

	/*bottom*/
	.bottomsubmitbtn {
		position: fixed;
		width: 100%;
		bottom: 0rpx;
		left: 0rpx;
		padding: 30rpx 50rpx;
		background-color: #FFFFFF;
		box-sizing: border-box;
		line-height: 100%;

		.zongji {
			font-size: 30rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
			color: #333333;
			line-height: 40rpx;
		}

		.price1 {
			font-size: 36rpx;
			font-family: PingFang SC-Heavy, PingFang SC;
			font-weight: 800;
			color: #FF5C00;
			line-height: 42rpx;
		}

		.submitcontext {
			background-color: rgba(255, 92, 0, 1);
			font-size: 32rpx;
			font-family: PingFang SC-Heavy, PingFang SC;
			font-weight: 800;
			color: #FFFFFF;
			line-height: 80rpx;
			border-radius: 120rpx;
			padding: 2rpx 48rpx;
			text-align: center;
		}
	}
</style>
